<?php
/**
 * 21天减重测评算法
 * Created by PhpStorm.
 * User: liao
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Algo;

use App\Interf\Calculable;
use TestingItemService;
use App\Models\LogHelper;

class OilTestAlgo extends BaseTestingAlgo implements Calculable
{

    public function __construct($tid, $answerStr, $typeFourExpress = null)
    {
        parent::__construct($tid, $answerStr);
    }

    public function calculate()
    {
        return $this->entrance($this->tid,$this->answers);
    }

    /**
     * 将拼装后的答案转换成字符串
     * @return string
     */
    public function resultToStr()
    {
        $final_ans = $this->getTopTwoOil($this->answers);
        $this->formateFinalAns($final_ans);
        $final_ans = $this->arrToStr($this->answers);
        LogHelper::log('oil final string answer ====>');
        LogHelper::log('$final_ans');
        return $final_ans;
    }

    /**
     * @param $final_ans
     * @return array
     */
    protected function formateFinalAns($final_ans)
    {
        for ($i = 0; $i < count($final_ans); $i++) {
            array_push($this->answers, $final_ans[$i]);
        }
        unset($final_ans);
        return $this->answers;
    }

    /**获取最终的前两位
     * @param $answers
     * @return array
     */
    public function getTopTwoOil($answers)
    {
        $targetArray = [];
        for ($i = 2; $i < count($answers); $i++) {
            array_push($targetArray, $answers[$i]['answer']);
        }
        $targetArray = implode('|||', $targetArray);
        $targetArray = explode("|||", $targetArray);
        $OilSortArray = $this->adduOilScore($targetArray);
        $topTwoArray = $this->sortArray($OilSortArray);
        return $topTwoArray;
    }

    /**根据和及优先级排序
     * @param $OilSortArray
     * @return array
     */
    public function sortArray($OilSortArray)
    {
        //获取所有的键
        $keys = array_keys($OilSortArray);
        //获取所有的值
        $vals = array_values($OilSortArray);
        //先对值排序,值相同时再对键排序
        array_multisort($vals, SORT_DESC, $keys, SORT_ASC);
        //将排序后的键和值重新组合成数组
        $OilSortArray = array_combine($keys, $vals);
        //返回排序的后前两项
        $SortArray = $this->removeSameKindOil($OilSortArray);
        return $SortArray;

    }

    /**根据类型排序返回最终结果
     * @param $OilSortArray
     * @return mixed
     */
    public function removeSameKindOil($OilSortArray)
    {
        $arrSaturated = array('e_h', 'f_d');
        $arrSingleSaturated = array('g_s', 'c_c', 'd_g', 'e_h');
        $arrMutilSaturated = array('b_k', 'a_y', 'f_d');
        $array_keys = array_keys($OilSortArray);
        $first_key=$array_keys[0];
        for($i=1;$i<4;$i++){
            if(in_array($first_key, $arrSaturated) && !in_array($array_keys[$i], $arrSaturated)){
                $seconde_key=$array_keys[$i];
                break;
            }elseif(in_array($first_key, $arrSingleSaturated) && !in_array($array_keys[$i], $arrSingleSaturated)){
                $seconde_key=$array_keys[$i];
                break;
            }elseif(in_array($first_key, $arrMutilSaturated) && !in_array($array_keys[$i], $arrMutilSaturated)){
                $seconde_key=$array_keys[$i];
                break;
            }
        }
        $data[0]=array(
            'index'=>6,
            'weight'=>$first_key,
            'answer'=>$OilSortArray[$first_key]
        );
        $data[1]=array(
            'index'=>7,
            'weight'=>$seconde_key,
            'answer'=>$OilSortArray[$seconde_key]
        );
        return $data;
    }

    /**求和
     * @param $targetArray
     * @return array
     */
    public function adduOilScore($targetArray)
    {
        $a_y = $b_k = $c_c = $d_g = $e_h = $f_d = $g_s = 0;

        if (in_array("心脑血管疾病", $targetArray)) {
            $a_y += 1;
            $b_k += 1;
            $d_g += 1;
            $g_s += 1;
        }
        if (in_array("糖尿病", $targetArray)) {
            $e_h += 1;
        }
        if (in_array("老年痴呆", $targetArray)) {
            $e_h += 1;
        }
        if (in_array("慢性消化系统疾病", $targetArray)) {
            $c_c += 1;
        }
        if (in_array("高胆固醇血症", $targetArray)) {
            $b_k += 1;
            $c_c += 1;
        }
        if (in_array("失眠症或神经衰落", $targetArray)) {
            $b_k += 1;
        }
        if (in_array("清炒", $targetArray)) {
            $a_y += 1;
            $b_k += 1;
            $f_d += 1;
        }
        if (in_array("炖煮", $targetArray)) {
            $a_y += 1;
            $b_k += 1;
            $e_h += 1;
        }
        if (in_array("炝炒", $targetArray)) {
            $e_h += 1;
            $g_s += 1;
        }
        if (in_array("凉拌", $targetArray)) {
            $d_g += 1;
        }
        if (in_array("煎炸", $targetArray)) {
            $e_h += 1;
            $g_s += 1;
        } else if (in_array("孕妇", $targetArray)) {
            $d_g += 1;
            $b_k += 1;
        }
        if (in_array("儿童", $targetArray)) {

            $d_g += 1;
        }
        if (in_array("老年人", $targetArray)) {
            $a_y += 1;
        }
        if (in_array("心脑血管疾病", $targetArray) || in_array("高血压病", $targetArray)) {
            $c_c = 0;
        }

        if (in_array("高胆固醇血症", $targetArray)) {
            $e_h = 0;
        }

        return $OilSortArray = array('a_y' => $a_y, 'b_k' => $b_k, 'c_c' => $c_c, 'd_g' => $d_g, 'e_h' => $e_h, 'f_d' => $f_d, 'g_s' => $g_s);


    }

    protected function entrance($tid,$answers){
        $totalScore =null;
//        foreach ($answers as $answer){
//            $totalScore = $totalScore + $answer['weight'];
//        }
        return $totalScore;
    }


}